package com.cjl.host.service;

import com.cjl.common.constants.HostFoodRelationsLogMessageConstants;
import com.cjl.common.dtos.ResponseResult;
import com.cjl.common.enums.HttpEnums;
import com.cjl.host.mapper.HostFoodRelationMapper;
import com.cjl.host.pojos.Host;
import com.cjl.host.pojos.HostFoodRelations;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Map;

/**
 * @author cjl
 * @version v1.0
 * @description TODO
 * @createTime 2023/2/17 11:34
 */

@Service
@Slf4j
public class HostFoodRelationsService {

    @Autowired
    HostFoodRelationMapper hostFoodRelationMapper;

    /**
     * 用户行为记录，记录用户对food数据库的更新操作
     *
     * @param dbHost          店主
     * @param responseResult  通用状态结果返回类
     * @param logMessage      日志信息
     * @param flag            操作标记： -1--查询操作   0--插入操作    1-删除操作    2-更新操作
     * @param behaviorMessage 行为信息
     */
    @Async
    public void hostFoodRelationRecord(Host dbHost, ResponseResult responseResult, String logMessage, int flag, String behaviorMessage) {
        //判断是否为查询操作
//        //数据库不保存查询的操作记录
//        if (flag == HostFoodRelationsLogMessageConstants.SELECT) return;

        log.info(Thread.currentThread().getName()+"。。。");
//        try {
//            Thread.currentThread().sleep(10000);
//        } catch (InterruptedException e) {
//            e.printStackTrace();
//        }
//        log.info(Thread.currentThread().getName()+"--10秒后。。。");
        //获取响应体参数
        Integer code = responseResult.getCode();
        String message = responseResult.getMessage();
        Map data = (Map) responseResult.getData();
        Object dbFoodId = null;
        if (data!=null)  dbFoodId = data.get("id");
        //操作成功
        if (code.equals(HttpEnums.SUCCESS.getCode())) {
            int foodId;
            if (dbFoodId == null) {
                log.error(logMessage, dbHost.getName());
                return;
            }
            foodId = Integer.parseInt(dbFoodId.toString());
            HostFoodRelations hostFoodRelations = HostFoodRelations.builder()
                    .updatedTime(new Date())
                    .hostId(dbHost.getId())
                    .foodId(foodId)
                    .behavior(dbHost.getName() + behaviorMessage + data.get("name"))
                    .type(flag)
                    .build();
            int result = hostFoodRelationMapper.insert(hostFoodRelations);
            if (result == 0) log.error(logMessage, dbHost.getName());

        } else {
            int foodId = HostFoodRelationsLogMessageConstants.DEFAULT_NULL_FOOD_ID;
            if (dbFoodId != null) foodId = Integer.parseInt(dbFoodId.toString());
            //操作失败
            HostFoodRelations hostFoodRelations = HostFoodRelations.builder()
                    .foodId(foodId)
                    .updatedTime(new Date())
                    .hostId(dbHost.getId())
                    .behavior(dbHost.getName() + "操作失败--具体原因：" + message)
                    .type(flag)
                    .build();
            int result = hostFoodRelationMapper.insert(hostFoodRelations);
            if (result == 0) log.error(logMessage, dbHost.getName());
        }
    }
}
